/*
 Developer: John Archer (john.archer.jpa@gmail.com)
 
 This file is part of VTBuilder.

 VTBuilder is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License.

 VTBuilder is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with VTBuilder.  If not, see <http://www.gnu.org/licenses/>.
 */
package core.tasks;

import core.para.Para;
import core.tasks.pack.PairCheckPack;
import gui.processor.Processor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;

public class PairCheck {

    public PairCheck() {
    }

    public boolean[] pegPairCheckNew(ArrayList<String> fReads, ArrayList<String> rReads, String bgRef, HashMap<String, ArrayList<Integer>> hsh, int startEnds[][], Processor p) throws Exception {
        int size = (fReads.size() / Para.TRDS);
        PairCheckPack[] mp = new PairCheckPack[Para.TRDS];
        CountDownLatch cdl = new CountDownLatch(Para.TRDS);
        for (int x = 0; x < Para.TRDS; x++) {
            if (x > 0) {
                mp[x] = new PairCheckPack(
                        cdl,
                        fReads.subList(x * size, (x * size + size)),
                        rReads.subList(x * size, (x * size + size)),
                        hsh,
                        bgRef.length(),
                        null);
            } else {
                mp[x] = new PairCheckPack(
                        cdl,
                        fReads.subList(x * size, (x * size + size)),
                        rReads.subList(x * size, (x * size + size)),
                        hsh,
                        bgRef.length(),
                        p);  
            }
            mp[x].start();
        }
        cdl.await();
        int[][] hits = new int[2][bgRef.length()];;
        for (PairCheckPack z : mp) {
            for (int x = 0; x < bgRef.length(); x++) {
                hits[0][x] = hits[0][x] + z.pOut[0][x];
                hits[1][x] = hits[1][x] + z.pOut[1][x];
            }
        }
        //go through each site within a start end bound and get if this segment is a mosaic or not
        boolean[] validSeg = new boolean[startEnds[0].length];
        for (int x = 0; x < startEnds[0].length; x++) {
            double fwd = 0;
            double rev = 0;
            for (int y = startEnds[0][x]; y < startEnds[1][x]; y++) {
                fwd = fwd + hits[0][y];
                rev = rev + hits[1][y];
            }
            if (rev > 0) {
                //System.out.println(fwd + " : " + rev);
                validSeg[x] = (rev / fwd) >= 0.1;
            } else {
                validSeg[x] = false;
            }
        }
        return validSeg;
    }
}